<?php

declare(strict_types=1);

namespace App\Controller\v1;

use App\Controller\AbstractController;
use App\Exception\BusinessException;
use App\Service\ArticleService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\DeleteMapping;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Annotation\PutMapping;
use Hyperf\HttpServer\Contract\RequestInterface;
use Psr\Http\Message\ResponseInterface;

class ArticleController extends AbstractController
{
    #[Inject]
    protected ArticleService $articleService;

    public function index(RequestInterface $request): array|ResponseInterface
    {
        $params = $request->all();
        try {
            $page = $request->input('page', 1);
            $size = $request->input('size', 20);
            $data =  $this->articleService->gerArticleList($page, $size, $params);
            return $this->success($data);
        }catch (BusinessException $e) {
            return $this->fail(40002,'获取文章列表失败',[ "errors" => $e->getMessage()]);
        }
    }

    /**
     * 创建文章
     * @param RequestInterface $request
     * @return array|ResponseInterface
     */
    #[PostMapping('')]
    public function store(RequestInterface $request): array|ResponseInterface
    {
        //获取参数
        $data = $request->all();
        try {
            $article =  $this->articleService->createArticle($data);
            return $this->success(['article' => $article], "文章创建成功");
        }catch (BusinessException $e){
            return $this->fail(40002,"文章创建失败",[ "errors" => $e->getMessage()]);
        }
    }

    /**
     * 获取单篇文章
     * @param int $id
     * @return array|ResponseInterface
     */
    #[GetMapping('{id}')]
    public function show(int $id): array|ResponseInterface
    {
        try{
            $article = $this->articleService->getArticleDetail($id);
            return $this->success(['article' => $article]);
        }catch (BusinessException $e){
            return $this->fail(40002,"获取文章信息失败",[ "errors" => $e->getMessage()]);
        }
    }

    /**
     * 更新文章
     * @param int $id
     * @param RequestInterface $request
     * @return array|ResponseInterface
     * @throws \App\Lib\ValidateException
     */
//    #[PutMapping('{id}')]
    public function update(int $id, RequestInterface $request): array|ResponseInterface
    {
        //获取参数
        $data = $request->all();
        try {
            $article = $this->articleService->updateArticle($id, $data);
            return $this->success(['article' => $article], "文章更新成功");
        }catch (BusinessException $e){
            return $this->fail(40002,"文章更新失败",[ "errors" => $e->getMessage()]);
        }
    }


    /**
     * 删除
     * @param int $id
     * @return array|ResponseInterface
     * @throws \Exception
     */
    #[DeleteMapping('{id}')]
    public function destory(int $id): array|ResponseInterface
    {
        try {
            $this->articleService->deleteArticle($id);
        }catch (BusinessException $e){
            return $this->fail(40002,"文章删除失败",[ "errors" => $e->getMessage()]);
        }
        return $this->success([], "文章删除成功");
    }

    /**
     * 点赞
     * @param int $id
     * @return array|ResponseInterface
     */
    public function like(int $id): array|ResponseInterface
    {
        try{
            $ret = $this->articleService->likeArticle($id);
            return $this->success($ret['data'], $ret['msg']);
        }catch (businessException $e){
            return $this->fail(40002,"点赞失败",[ "errors" => $e->getMessage()]);
        }
    }

    public function favorite(int $id): array|ResponseInterface
    {
        try{
            $ret = $this->articleService->favoriteArticle($id);
            return $this->success($ret['data'], $ret['msg']);
        }catch (businessException $e){
            return $this->fail(40002,"收藏失败",[ "errors" => $e->getMessage()]);
        }
    }
}